// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Piston Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 68 : TYPE_PISTON

private alias object.value3 : object.originY

// Function declarations
reserve function Piston_DebugDraw
reserve function Piston_DebugSpawn

// Static Values
public value Piston_value39 = 0;

// Tables
public table Piston_table45
	0x380000, 0x400000, 0x600000
end table


function Piston_DebugDraw
	temp0 = object[0].value17
	temp0 -= Piston_value39
	DrawSprite(0)
end function


function Piston_DebugSpawn
	temp0 = object[0].value17
	temp0 -= Piston_value39
	CreateTempObject(TypeName[Piston], temp0, object.xpos, object.ypos)
	GetTableValue(object[tempObjectPos].value0, object[tempObjectPos].propertyValue, Piston_table45)
end function


event ObjectMain
	if object.value2 != 0
		object.value2--
		object.yvel = 0
	else
		if object.direction == FLIP_NONE
			object.yvel = 0x80000
			object.value1 += object.yvel
			if object.value1 >= object.value0
				object.direction ^= FLIP_X
				if object.propertyValue != 0
					object.value2 = 60
				end if
			end if
		else
			if object.propertyValue != 0
				object.yvel = -0x80000
			else
				object.yvel = -0x10000
			end if
			object.value1 += object.yvel
			if object.value1 <= 0
				object.direction ^= FLIP_X
				object.value2 = 60
			end if
		end if
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0		
			BoxCollisionTest(C_BOX2, object.entityPos, -28, -32, 28, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 1
				object[currentPlayer].ypos += object.yvel
				break
			case 4
				if object[currentPlayer].gravity == GRAVITY_GROUND
					if object.yvel > 0
						CallFunction(PlayerObject_Kill)
					end if
				end if
				break
			end switch
		end if	
		if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
			if object[0].value1 >= 17
				BoxCollisionTest(C_TOUCH, object.entityPos, -28, -32, 28, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1

					if object.ypos > object.originY
						object[currentPlayer].ypos = object.originY
					end if

					if object[currentPlayer].xpos > object.xpos
						object[currentPlayer].ixpos = object[currentPlayer].collisionLeft
						FlipSign(object[currentPlayer].xpos)
						object[currentPlayer].xpos += 0x280000
						object[currentPlayer].xpos += object.xpos
						if object[currentPlayer].xvel < 0
							object[currentPlayer].pushing = 2
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
						end if
					else
						object[currentPlayer].ixpos = object[currentPlayer].collisionRight
						FlipSign(object[currentPlayer].xpos)
						object[currentPlayer].xpos -= 0x280000
						object[currentPlayer].xpos += object.xpos
						if object[currentPlayer].xvel > 0
							object[currentPlayer].pushing = 2
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
						end if
					end if
				end if
			end if	
		end if				
	next
	object.ypos += object.yvel
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-28, -32, 56, 64, 132, 1)
	foreach (TypeName[Piston], arrayPos0, ALL_ENTITIES)
		GetTableValue(object[arrayPos0].value0, object[arrayPos0].propertyValue, Piston_table45)
		object[arrayPos0].originY = object[arrayPos0].ypos
	next
	temp0 = 0
	Piston_value39 = DebugMode_ObjCount
	while temp0 < 3
		SetTableValue(TypeName[Piston], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Piston_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Piston_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
